t
Creating a Class-based Simulation

A discrete-event*269* simulation runs in time slices, from state to state. Most discrete-event simulations track the activity of individuals acting and reacting within an environment. Together, the entities acting within the environment along with the environment are described as a system. 

A discrete-event simulation might model a gas-station system—studying the behavior of cars in and out of lines at the pumps. Or it might study the movement and interactions of molecules of gas in a jar. Your simulation looks at the behavior of animals in a barnyard.

In all these cases, it is intuitively clear that an object-oriented approach to programming*851* lends itself to explicit software modeling of the real-world entities described in such systems. So far, you have developed some of the basic data structures that describe the barnyard system. Now it is time to bring it to life with some methods and additional classes.

u
Open the Barnyard Attributes window and open a Value window on the activeYards class attribute. Click the Graphic check box. Delete three of the four string elements in the activeYards list. Double-click the remaining string element to open its Value window.

u
In the Value of List Item 1 window, scroll upward in the Data Types list and click the Barnyard item. The elementary data type string element is now transformed into an instance of class Barnyard.

u
Option-click the OK button in the Value window to update the attribute values and close both Value windows. The Barnyard Attributes window now shows a Barnyard instance as the first element in activeYards.

u
Execute the inspect yards universal method to confirm the change.

An interesting reflexive relationship occurs when a *93*class attribute holds instances of its own class. 

u
Open the Barnyard Attributes window. Open the Value of Attribute activeYards window. Next, open the Barnyard item in the activeYards list. The Barnyard instance’s own activeYards class attribute shows a list with the instance itself as the class attribute’s first item.

You can, in fact, get into an endless loop opening the activeYards attribute of each Value of List Item 1 window that you open in this manner. Here the activeYards class attribute has been opened three deep.

Opening such a reflexive relationship does not cause an error. Most likely, it is simply confusing and causes screen clutter. You do notice, however, that Prograph neatly stacks your progressive opening of Value windows. And as with all stacks of modal Value windows, an Option-click on the top window’s OK or Cancel button closes the entire stack.*1083*

The Application System class uses this reflexive construct. An instance of Application is stored in Application’s class attribute current. When you build an application in Application Builder, you are thus creating an instance stored in current. It is important for you to become comfortable inspecting such reflexive*941* object structures.

===========================================================
TIP:  The Value window titles are extremely helpful in avoiding a “looping inspection” of class attributes that have instances of their own class stored in them.
===========================================================

The direct inspection and editing of class structures, especially attribute default and instance values, is an important skill to refine as you become a Prographacker. The runtime debugging facilities you studied in the first tutorial are complemented by these class- and object-inspection and editing capabilities.

You continue to practice these object-inspection*768* and editing skills as you get to the business of creating a collection of animals to populate the Farmer’s barnyard. So you don’t get bogged down writing the event-processing loop that runs the simulation, refresh your skill at selectively loading files as you bring in some prewritten methods for the Barnyard Simulation.

The*973* Selective Load option is available in the standard file-opening dialog accessed by the Open… item of the File menu. This option makes it easy to share and reuse classes and methods in your Prograph applications. Selective loading is context sensitive. The active window at the time you select Open… determines whether classes or methods are loadable. 

u
Click to activate the Universal Methods window and select Open… from the File menu. Click the Selective Load check box. Locate and select the file, Chapter 6 Helpers, in the Ch 6 ƒ folder (inside the Tutorial Examples ƒ folder). Press Return to open the Methods To Load dialog. Find, then Shift-click to select both the random from list and simulation methods in the scrolling list.

u
Press Return to load these universal methods. When Prograph is finished loading, you see these icons in the Universal Methods window.

u
Open the Methods window of the Barnyard class. Selectively load the Barnyard/action method from the Chapter 6 Helpers file.

You now have the simulation “engine” in place. The three methods you just loaded implement the activity scenario laid out in the narrative description of the design goal. The simulation universal method endlessly repeats a local method, do next time slice, which simply tells each active Barnyard instance to take action.

The heart of the simulation, deciding what type of activity is to be performed and creating Diary entries, is done in the action method of Barnyard. If you are curious, you can inspect the inner workings of the action method to see how the narrative design goal was turned into a Prograph method. However, you need only know that it works. You can forge onward. To start running the Barnyard Simulation, you need two additional core classes:

u
Activate the Classes window and create a new class called DiaryEntry. Add a single attribute, what happened, and set its default data type to string. 

As each round of activity in a Barnyard instance begins, a DiaryEntry instance is created. As activity occurs, a descriptive sentence is appended to the what happened attribute of the DiaryEntry instance. At the end of a round of activity, the content of the current what happened attribute of the DiaryEntry is displayed for review in a Value window. Then the DiaryEntry instance is added to the end of the diary attribute of the active Barnyard. 

To see this in action, you need to start creating some barnyard animals.

u
Create a new class, Animal, in the Classes window. Give it the attributes, name, age, and food, with the following default values " " (an empty string), 1 (the simulation assumes all animals are at least a year old), and "generic chow", respectively.

Your animals know how to move, talk, eat, sleep, and have birthdays. To save you the trouble of creating methods to implement these behaviors, selectively load them, as follows:

u
Open the Animal Methods window and have it active before performing the selective load as usual. This ensures that the methods you select in the Methods To Load dialog are added to the Animal class.

u
Shift-click to select all methods prefixed with Animal/ in the dialog scrolling list, except the Animal/<<>> item (you get to that one later), and press Load.

The Barnyard Simulation is ready to go. Granted, you have a very undifferentiated kingdom, with all Barnyard creatures being simply animals—not cows and pigs or what have you. But it’s a start.

u
Select*668* Universal Methods *1122*from*1189* the Windows menu. Select the simulation method in the Universal window, and then choose Set Program from the Exec menu. This lets you easily run the Barnyard Simulation with a Command-R command.

Before running the simulation, you need to “prime the pump” by putting a couple of Animal instances into the active Barnyard.
u
Open the Barnyard Attributes window, and then open a Value window on the activeYards class attribute. Open the Barnyard instance that is Item 1 in the activeYards list.

u
Open a Value window on the inhabitants instance attribute of the Barnyard instance. Click Graphic and add three items to the inhabitants list.

u
Open each NULL item in the inhabitants list and select Animal to transform each into an Animal instance with attribute values such as:

 Item 1 Animal
  name: Harvey
  age: 7
   food: pellets

 Item 2 Animal
  name: Sally
  age: 2
   food: grass

 Item 3 Animal
  name: Timmy
  age: 3
   food: oats and hay

When you have finished editing, the Value of Attribute inhabitants window looks like the following:

u
Option-click OK to close the Value windows, then start the simulation by pressing Command-R.

u
The simulation begins with a diary-entry description of what is going on in the barnyard at the time of the anthropologist’s first observation. The Value window you see is produced during execution of the Barnyard/action method when a display primitive receives the what happened attribute of the current DiaryEntry as input. (The activity description in your Value window might vary from the one below. The random from list method is used to determine the action each animal randomly performs at the time of observation.)

u
Press Return to proceed to the next round of events. Continue pressing Return and review the diary reports as they are produced.

Every fourth iteration of the Barnyard/action method skips the routine activity summary and produces the special birthday-and-shopping event noted at the outset description of the Barnyard Simulation. A series of four simple dialogs prompt you to find out what kind of animal the farmer bought at the auction.

u
To produce a new Animal instance to be added to the inhabitants of the Barnyard instance in activeYards, respond as follows:

 What kind of animal did the farmer buy?

   Animal

 What name did the farmer give the Animal?

   Albert

 Please enter the Animal’s age.

   2

 What is the Animal’s favorite food?

   oats and hay

The simulation creates the new Animal and adds it to the active Barnyard’s inhabitants attribute list. The Value window on the current DiaryEntry then reports on the animals’ birthdays and on the acquisition of the new Animal at the auction. Continue running the simulation for a while, adding a new Animal each year. 

If you make a mistake and don’t type exactly the class name Animal in the What kind of animal did the farmer buy? dialog, an error message appears after the last dialog, noting that That class does not exist. Do you want to create it? Press Cancel and then Command-/ to abort execution. To start again, use Command-R. Run the simulation for a while, adding a new Animal each year. When you are ready to stop:

u
Use the*106* Break command (Command-.) immediately after dismissing a Value window displaying a DiaryEntry. The Stack window and an execution window on the currently executing method appear.

u
Select*10* Abort *329*from the Exec menu to stop running the simulation. The previously open editor windows reappear. Open the Barnyard instance in the activeYards list to inspect the content of the instance’s inhabitants and diary attributes.

Inspecting the inhabitants and diary attributes of the Barnyard instance in activeYards convinces you that the simulation is indeed working.